package com.kou.service;

import com.kou.dao.UserDao;
import com.kou.dao.UserDaoImpl;
import com.kou.pojo.User;
import com.kou.utils.Base64Utils;
import com.kou.utils.EmailUtils;
import com.kou.utils.MD5Utils;
import com.kou.utils.RandomUtils;

/**
 * @author lixu
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();
    /**
     * 根据用户名查询用户对象
     * @param username
     * @return
     */
    @Override
    public User findUserByUsername(String username) {
        return userDao.findUserByUsername(username);
    }

    /**
     * 注册
     * @param username
     * @param password
     * @param email
     * @param gender
     */
    @Override
    public void register(String username, String password, String email, String gender) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(MD5Utils.md5(password));//加密
        user.setEmail(email);
        user.setGender(gender);
        //
        user.setStatus("0");//未激活
        user.setRole(1);//会员 消费者
        //生成激活码
        String code = RandomUtils.createActive();
        user.setCode(code);
        //保存用户信息 （包含激活码——）
        userDao.register(user);
        //发邮箱
        EmailUtils.sendEmail(user);
    }

    /**
     * 激活
     * @param email
     * @param code
     */
    @Override
    public void activate(String email, String code) {

        //解密
        String c = Base64Utils.decode(code);
        userDao.activate(email,c);
    }

    @Override
    public User login(String username, String password) {
        password = MD5Utils.md5(password);
        return userDao.login(username,password);
    }

    @Override
    public String findUserName(int uid) {
        return userDao.findUserName(uid);

    }
}
